//
//  FMCellViewController.m
//  PsychologyFM
//
//  Created by 杜建虎 on 16/1/14.
//  Copyright © 2016年 ZhangLuDu. All rights reserved.
//

#import "FMCellViewController.h"
#import "FMFindCellModel.h"
#import "PKRequestManager.h"
#import "FMSelectTableViewCell.h"
#import "FMDiantaiPlayViewController.h"
@interface FMCellViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *dataArr; // tab 数据
@property (nonatomic, strong)UITableView *tableView;
@end

@implementation FMCellViewController

- (void)viewDidLoad {
    _dataArr = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = YES;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self addTabview];
    [self addrequestData];
    self.navigationController.title = _topTitle;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 2, 30, 60, 15)];
    titleLabel.textColor = [UIColor orangeColor];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.text = _topTitle;
    self.navigationItem.titleView = titleLabel;
    
    
}

- (void)addrequestData
{
    [PKRequestManager requestWithUrlString:_myTag parDic:nil method:POST finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *array = dic[@"data"];
        for (NSDictionary *Dic in array) {
            FMFindCellModel *model = [[FMFindCellModel alloc] init];
            [model setValuesForKeysWithDictionary:Dic];
            [_dataArr addObject:model];
        }
        [_tableView reloadData];
        
    } error:^(NSError *error) {
        
    }];
}

- (void)addTabview
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 52, kScreenWidth, kScreenHeight - 60)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 70;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[FMSelectTableViewCell class] forCellReuseIdentifier:@"FMSelectTableViewCell"];
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"く" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftItem:)];
    self.navigationItem.leftBarButtonItem = leftItem;
}

- (void)leftItem:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


#pragma --- tableView代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FMSelectTableViewCell"];
    FMFindCellModel *model = _dataArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FMDiantaiPlayViewController *dianTaiVC = [[FMDiantaiPlayViewController alloc] init];
    FMFindCellModel *model = _dataArr[indexPath.row];
    dianTaiVC.value = model.ID;
    dianTaiVC.rootVC = _rootVC;
    [self.navigationController pushViewController:dianTaiVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 68;
}



- (void)viewWillDisappear:(BOOL)animated
{
    _rootVC.scrollView.height -= 49;
    [_rootVC.view addSubview:_rootVC.tabbar];
    
    
    [self showTabbar];
}

- (void)showTabbar
{
    self.rootVC.tabbar.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.automaticallyAdjustsScrollViewInsets  = NO;
    
    _rootVC.scrollView.height += 49;
    //    _rootVC.tabbar.hidden = YES;
    [_rootVC.tabbar removeFromSuperview];
}

@end
